﻿
using Dapper;
using HBase;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Diagnostics;
using System.Linq;
using static System.Collections.Specialized.BitVector32;
using static System.Net.WebRequestMethods;
namespace hCommand {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public class AbsGroupRedirectEngine<CS>:AbsTaskComp where CS : AbsCoreStock {
        public long paramId;

      
        protected override TransRes Execute() {
            CoreTaskParam ctp = getById<CoreTaskParam>(paramId);
            if (ctp == null) {
                return new TransRes("任务不存在");
            }
            if (ctp.status > (int)CoreTaskParamStatus.执行结束) {
                return new TransRes("任务已经结束");
            }
            var res = checkType(ctp);
            if (res != null) {
                return res;
            }
            CoreTask ct = getById<CoreTask>(ctp.taskId);
            CS cs = getById<CS>(ct.stockId);
            if (cs.id == 0) {
                return new TransRes("stock 为空");
            }
            int  src=  getGroupLoc( ctp); 
            if(src==0){ 
                return noCHange(ctp);
            }
            CoreWhLoc loc = selection.changeLocSelect(this,src,cs);  
            if (loc == null) {
               return noCHange(ctp);
            } 
            int oldDes=   ctp.des;
            ctp.des = (int)loc.id;
            ctp.status = (int)CoreTaskParamStatus.可以接收任务;
            update(ctp,x => new { x.des,x.status });
            LocCoreUtil.UpdataWhLoc(this,oldDes,ct.diff);
            LocCoreUtil.UpdataWhLoc(this,(int)loc.id,ct.diff);
       
            return new TransRes();
        }
        TransRes noCHange( CoreTaskParam ctp){ 
              ctp.status = (int)CoreTaskParamStatus.可以接收任务;
              update(ctp,x => new { x.status }); 
              return new TransRes();
         }

        protected virtual TransRes checkType(CoreTaskParam ctp) {
             if (ctp.type != (int)TaskParamType.进组重定&&ctp.type != (int)TaskParamType.组内移位)
            {
                return new TransRes(ctp.id+"库位预分配 类型错误"+ctp.type);
            }
            return null;
        }
        protected virtual int getGroupLoc(CoreTaskParam ctp) {
             int whLoc=0;  
            if(ctp.type == (int)TaskParamType.进组重定){ 
                  whLoc =  ctp.des;
            }else if(ctp.type == (int)TaskParamType.组内移位){ 
                  whLoc = ctp.src;
            }
            return whLoc;
           
        }


    }
}
